package java6703.lesson13;

public class ThreadAndRunnable6703 {
    public static void main(String[] args) {
        //调用saleByThread6703
//        saleByThread6703();

        //调用saleByRunnableXXXX
        saleByRunnable6703();
    }

    private static void saleByRunnable6703() {
        TicketRunnable6703 task = new TicketRunnable6703();
        new Thread(task, "陈杰1").start();
        new Thread(task, "陈杰2").start();
        new Thread(task, "陈杰3").start();
    }


    //方法：saleByThread6703，启动三个线程一起售票
    private static void saleByThread6703() {

        //实例化三个TicketThread6703
        TicketThread6703 task1 = new TicketThread6703("陈杰1");
        TicketThread6703 task2 = new TicketThread6703("陈杰2");
        TicketThread6703 task3 = new TicketThread6703("陈杰3");

        //调用三个实例的start()方法
        task1.start();
        task2.start();
        task3.start();
    }
}


class TicketThread6703 extends Thread {

    //私有变量tickets在类中声明int类型的私有变量tickets，表示已售票的数量
    private int tickets = 1;

    //构造方法为类定义一个带String参数的构造方法，用于初始化线程的名称
    public TicketThread6703(String name) {
        super(name);
    }

    @Override
    //重写run()方法循环售票
    public void run() {
        super.run();
        while (tickets <= 8) {
            String name = Thread.currentThread().getName();

            //tickets加1表示售出一张票每售出一张票，输出当前售票窗口的名称和售票数
            System.out.println(name + "：第" + tickets++ + "张票");
        }
    }
}


class TicketRunnable6703 implements Runnable {
    private int tickets = 1;

    @Override
    public void run() {
        while (tickets <= 8) {
            String name = Thread.currentThread().getName();
            System.out.println(name + "：第" + tickets++ + "张票");
        }
    }
}
